package com.itqf.controller;

import com.itqf.exception.LoginException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * projectName: springmvc2103
 *
 * @author: 赵伟风
 * time: 2021/7/5 15:17
 * description:
 */
@ControllerAdvice
public class ControllerExceptionHandler {


    /**
     * ExceptionHandler会指定当前方法收集异常类型
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(ArithmeticException.class)
    public Object handleException1(ArithmeticException ex)
    {
        System.out.println("全局异常:ex = " + ex);

        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg","全局异常处理!全局的算数异常!");

        return map ;
    }

    @ResponseBody
    @ExceptionHandler(NullPointerException.class)
    public Object handleException2(NullPointerException ex)
    {


        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg","全局异常处理!全局的空指针!");

        return map ;
    }


    @ResponseBody
    @ExceptionHandler(LoginException.class)
    public Object loginError(LoginException ex)
    {


        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg","登录失败!");

        return map ;
    }


}
